package com.rytong.tools.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioGroup;

public class LPSegmentLayout extends Component {

    public ArrayList<LPSegment> segmentList_ = new ArrayList<LPSegment>(2);

    public class MyLPSegmentLayout extends RadioGroup implements CompositedComponent {

        public MyLPSegmentLayout(Context context) {
            super(context);
            setOrientation(RadioGroup.HORIZONTAL);
            setGravity(Gravity.CENTER);
        }

        @Override
        public Component composited() {
            return LPSegmentLayout.this;
        }
    }

    @Override
    public void initRealView(Activity activity, String nodeValue) {
        realView_ = new MyLPSegmentLayout(activity);
    }

    @Override
    public void mouldH() throws Exception {
    	super.mouldH();
        int size = segmentList_.size();
        LPSegment child;
        ViewGroup parent;
        LayoutParams params = new LayoutParams(width_, height_);
        MyLPSegmentLayout segmentLayout = null;
        if (null != realView_)
            segmentLayout = (MyLPSegmentLayout) realView_;
        for (int i = 0; i < size; i++) {
            child = segmentList_.get(i);
            child.groupNum_ = i;
            child.groupCount_ = size;
            child.width_ = width_ / size;

            child.setSegmentIcon();
            params.width = child.width_;
            child.setLayoutParams(params);
            child.segmentList_ = segmentList_;
        }
    }

    public void addView(Component comp) {
        if (null != realView_) {
            MyLPSegmentLayout temp = (MyLPSegmentLayout) realView_;
            if (null != comp.realView_)
                temp.addView(comp.realView_);
        }
    }
    
    @Override
    public void releaseResource(Component component) {
    	super.releaseResource(component);
    	if(segmentList_ != null){
    		segmentList_.clear();
    		segmentList_ = null;
    	}
    }
}
